using System;
using System.DHTML;
using Ext;

namespace SampleScripts.simple_widgets
{
	public class ProgressBarScript
	{
		public static void main(Dictionary args)
		{
			ExtClass.onReady(new AnonymousDelegate(delegate { new ProgressBarScript().init(); }));
		}

		public void init()
		{
			//==== Progress bar 1 ====
			ProgressBar pbar1 = new ProgressBar(new ProgressBarConfig()
            	.custom("text", "Initializing...")
            	.ToDictionary());

			Element btn1 = ExtClass.get("btn1");
			btn1.on("click", new AnonymousDelegate(delegate {
				ExtClass.fly("p1text").update("Working");
				if (!pbar1.rendered) {
					pbar1.render("p1");
				}
				else
				{
					Type.SetField(pbar1, "text", "Initializing...");
					pbar1.show();
				}

				Runner.run(pbar1, ExtClass.get("btn1"), 10, delegate {
					pbar1.reset(true);
					ExtClass.fly("p1text").update("Done.").show();
				});
			}));	
	
			//==== Progress bar 2 ====
			ProgressBar pbar2 = new ProgressBar(new ProgressBarConfig()
            	.custom("text", "Ready")
            	.id("pbar2")
            	.cls("left-align")
            	.renderTo("p2")
            	.ToDictionary());

			Element btn2 = ExtClass.get("btn2");
			btn2.on("click", new AnonymousDelegate(delegate {
				Runner.run(pbar2, btn2, 12, delegate {
					pbar2.reset();
					pbar2.updateText("Done.");
				});
			}));

			//==== Progress bar 3 ====
			ProgressBar pbar3 = new ProgressBar(new ProgressBarConfig()
            	.id("pbar3")
            	.width(300)
            	.renderTo("p3")
            	.ToDictionary());

			pbar3.on("update", new ProgressBarUpdateDelegate(delegate {
				//You can handle this event at each progress interval if
				//needed to perform some other action
				ExtClass.fly("p3text").dom.InnerHTML += ".";
			}));

			Element btn3 = ExtClass.get("btn3");
			btn3.on("click", new AnonymousDelegate(delegate {
				ExtClass.fly("p3text").update("Working");
				btn3.dom.Disabled = true;
            	pbar3.wait(new Dictionary(
            	           	"interval", 200,
            	           	"duration", 5000,
            	           	"increment", 15,
            	           	"fn", new AnonymousDelegate(delegate
            	           		{
            	           			btn3.dom.Disabled = false;
            	           			ExtClass.fly("p3text").update("Done");
            	           		})));
			}));

			//==== Progress bar 4 ====
			ProgressBar pbar4 = new ProgressBar(new ProgressBarConfig()
				.custom("text", "Waiting on you...")
				.id("pbar4")
				.custom("textEl", "p4text")
				.cls("custom")
				.renderTo("p4")
				.ToDictionary());

			Element btn4 = ExtClass.get("btn4");
			btn4.on("click", new AnonymousDelegate(delegate {
				Runner.run(pbar4, btn4, 19, delegate {
					pbar4.updateText("All finished!");
				});
			}));
		}
	}

	internal class Runner
	{
		private static Callback f(int v, ProgressBar pbar, Element btn, int count, AnonymousDelegate onComplete)
		{
			return delegate { 
				if (v > count)
				{
					btn.dom.Disabled = false;
					onComplete();
				}
				else
				{
					double i = v/count;
					if (pbar.id == "pbar4")
					{
						//give this one a different count style for fun
						pbar.updateProgress(i, Math.Round(100*i) + "% completed...");
					}
					else
					{
						pbar.updateProgress(i, String.Format("Loading item {0} of {1}...", v, count));
					}
				}
			};
		}

		public static void run(ProgressBar pbar, Element btn, int count, AnonymousDelegate onComplete)
		{
			btn.dom.Disabled = true;
			int ms = 5000/count;
			for (int i = 1; i < (count + 2); i++)
			{
				Window.SetTimeout(f(i, pbar, btn, count, onComplete), i*ms);
			}
		}
		
	}
}
